package com.gitee.stream;

import com.alibaba.fastjson.JSONObject;
import com.gitee.stream.cart.CartService;
import com.gitee.stream.cart.Sku;
import com.gitee.stream.cart.SkuCategoryEnum;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * 对比 原始集合操作与Stream集合操作
 *
 * @author gcq
 * @Create 2022-06-27
 */
public class StreamVs {

    /**
     * 1、想看看购物车中有什么商品
     * 2、图书类的商品都买
     * 3、其余的商品买两件最贵的
     * 4、只要两件商品的名称和总价
     */
    @Test
    public void oldCartHandler() {
        // 1
        List<Sku> cartSkuList = CartService.getCartSkuList();
        System.out.println(JSONObject.toJSONString(cartSkuList, true));

        // 2
        List<Sku> skuList = new ArrayList<>();
        for (Sku sku : cartSkuList) {
            if(sku.getSkuCategory().equals(SkuCategoryEnum.BOOKS)) {
                skuList.add(sku);
            }
        }
        System.out.println(JSONObject.toJSONString(skuList, true));


        // 3
        cartSkuList.sort(new Comparator<Sku>() {
            @Override
            public int compare(Sku s1, Sku s2) {
                if(s1.getTotalPrice() > s2.getTotalPrice()) {
                    return -1;
                } else if(s1.getTotalPrice() < s2.getTotalPrice()) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });

        List<Sku> newSkuList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            newSkuList.add(cartSkuList.get(i));
        }
        System.out.println("=============");
        System.out.println(JSONObject.toJSONString(newSkuList, true));

        // 4
        Double money = 0.0;
        for (Sku sku : newSkuList) {
            money += sku.getTotalPrice();
        }
        System.out.println(money);

        List<String> nameList = new ArrayList<>();
        for (Sku sku : newSkuList) {
            nameList.add(sku.getSkuName());
        }
        System.out.println(JSONObject.toJSONString(nameList, true));
    }


    /**
     * 以Stream流方式实现需求
     */
    @Test
    public void newCardHandle() {
        AtomicReference<Double> money = new AtomicReference<>(Double.valueOf(0.0));
        List<String> resultSkuNameList = CartService.getCartSkuList()
                .stream()
                /**
                 * 打印商品信息
                 */
                .peek(sku -> System.out.println(JSONObject.toJSONString(sku, true)))
                /**
                 * 过滤掉所有图书类商品
                 */
                .filter(sku -> !SkuCategoryEnum.BOOKS.equals(sku.getSkuCategory()))
                /**
                 * 基于价格排序
                 */
                .sorted(Comparator.comparing(Sku::getTotalPrice).reversed())
                /**
                 * 两件最贵的
                 */
                .limit(2)
                /**
                 * 累计商品总价
                 */
                .peek(sku -> money.set(money.get() + sku.getTotalPrice()))
                /**
                 * 商品名称
                 */
                .map(sku -> sku.getSkuName())
                /**
                 * 收集结果
                 */
                .collect(Collectors.toList());
        /**
         * 打印结果
         */
        System.out.println(JSONObject.toJSONString(resultSkuNameList, true));
        /**
         * 总价格
         */
        System.out.println("商品总价:" + money.get());
    }

    public void test(){
        Sku sku = new Sku();
    }
}